package edu.tsinghua.mrpvc.simulate;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;


import edu.tsinghua.mrpvc.device.AirConDevice;
import edu.tsinghua.mrpvc.device.CameraDevice;
import edu.tsinghua.mrpvc.device.CurtainDevice;
import edu.tsinghua.mrpvc.device.FrigeDevice;
import edu.tsinghua.mrpvc.device.LightDevice;
import edu.tsinghua.mrpvc.device.LightSensor;
import edu.tsinghua.mrpvc.device.MicDevice;
import edu.tsinghua.mrpvc.device.MobileDevice;
import edu.tsinghua.mrpvc.device.RFIDDetect;
import edu.tsinghua.mrpvc.device.ThermoSensor;
import edu.tsinghua.mrpvc.device.TvDevice;
import edu.tsinghua.mrpvc.device.simulate.CameraSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.FrigeSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.LightSensorSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.MicSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.MobileSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.PhoneSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.RFIDSimulateTopic;
import edu.tsinghua.mrpvc.device.simulate.ThermoSensorSimulateTopic;


public class Simulater extends JFrame {
	private ServiceReference ref = null;
	private BundleContext context = null;
	private ServiceTracker tracker;
	public Simulater(BundleContext context){
		super("Simulater");
		this.context = context;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doRFID(), BorderLayout.NORTH);
		panel.add(doThermoSensor(), BorderLayout.CENTER);
		panel.add(doLightSensor(), BorderLayout.WEST);
		panel.add(doLight(), BorderLayout.EAST);
		panel.add(doCurtainAndAirCon(), BorderLayout.SOUTH);
		getContentPane().add(panel);
		pack();
		setVisible(true);
	}
	
	private JPanel doCurtainAndAirCon(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doCurtain(), BorderLayout.WEST);
		panel.add(doAirCon(), BorderLayout.CENTER);
		panel.add(doRFIDEvent(), BorderLayout.EAST);
		panel.add(doThermoEvent(), BorderLayout.NORTH);
		panel.add(doOtherEvent(), BorderLayout.SOUTH);
		//panel.add(doSetLight(), BorderLayout.EAST);
		return panel;		
	}
	
	private JPanel doOtherEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doLightSensorEvent(), BorderLayout.WEST);
		panel.add(doLightEvent(), BorderLayout.EAST);
		panel.add(doCurtainEvent(), BorderLayout.CENTER);
		panel.add(doAirconEvent(), BorderLayout.SOUTH);
		return panel;		
	}
	
	private JPanel doAirconEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doAirconTuneEvent(), BorderLayout.NORTH);
		panel.add(doAirconIsValidEvent(), BorderLayout.WEST);
		panel.add(doAirconPowerEvent(), BorderLayout.CENTER);
		panel.add(doAirconModeEvent(), BorderLayout.EAST);
		panel.add(doAirconOtherEvent(), BorderLayout.SOUTH);
		return panel;		
	}
	
	private JPanel doAirconOtherEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doAirconWindEvent(), BorderLayout.WEST);
		panel.add(doAirconTmpEvent(), BorderLayout.CENTER);
		panel.add(doMicEvent(), BorderLayout.EAST);
		panel.add(doTvAction(), BorderLayout.NORTH);
		panel.add(doOtherEvent1(), BorderLayout.SOUTH);
		return panel;			
	}
	
	private JPanel doOtherEvent1(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doCameraEvent(), BorderLayout.NORTH);
		panel.add(doPhoneEvent(), BorderLayout.WEST);
		panel.add(doMobile(), BorderLayout.CENTER);
		panel.add(doFrige(), BorderLayout.EAST);
		return panel;			
	}
	
	private JPanel doFrige(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doFrigeIsValidEvent(), BorderLayout.EAST);
		panel.add(doFrigeGetTmp(), BorderLayout.WEST);
		panel.add(doFrigeFoodOutEvent(), BorderLayout.CENTER);
		panel.add(doFrigeFoodInEvent(), BorderLayout.NORTH);
		panel.add(doFrigeGetFood(), BorderLayout.SOUTH);
		return panel;			
	}
	
	private JPanel doFrigeGetFood(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Frige: GetFood");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(FrigeDevice.class.getName());
				FrigeDevice frige = (FrigeDevice) context.getService(ref);
				String[] foodInfo = frige.getFoodInfo();
				System.out.print("Food: ");
				for(int i = 0; i < foodInfo.length; i++){
					System.out.print(foodInfo[i]);
				}
				System.out.println();
				
			}			
		});
		return panel;
	}
	private JPanel doFrigeFoodInEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Frige: FoodIn");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					
					Map<String, String> property = new Hashtable<String, String>();
					property.put(FrigeSimulateTopic.PROPER_FOOD_NAME, "Apple");
					Event event = new Event(FrigeSimulateTopic.FRIGE_FOOD_IN, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;
	}
	private JPanel doFrigeFoodOutEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Frige: FoodOut");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(FrigeSimulateTopic.PROPER_FOOD_NAME, "Apple");
					Event event = new Event(FrigeSimulateTopic.FRIGE_FOOD_OUT, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	} 
	
	private JPanel doFrigeGetTmp(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Frige: getTemperature");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(FrigeDevice.class.getName());
				FrigeDevice frige = (FrigeDevice) context.getService(ref);
				float tmp = frige.getTemperature();
				System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
						" Server: Frige's temperature is " + tmp);
			}			
		});
		return panel;		
	} 
	private JPanel doFrigeIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Frige: IsValid");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(FrigeDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	} 
	
	private JPanel doMobile(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doMobileCtrlTvEvent(), BorderLayout.NORTH);
		panel.add(doMobileSharePathEvent(), BorderLayout.WEST);
		panel.add(doMobileIsValidEvent(), BorderLayout.CENTER);
		return panel;			
	}
	
	private JPanel doMobileIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Mobile: IsValid");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(MobileDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doMobileSharePathEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Mobile: SharePath");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(MobileSimulateTopic.MOBILE_SHARE_PATH, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	
	private JPanel doMobileCtrlTvEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Mobile: CtrlTv");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(MobileSimulateTopic.MOBILE_CTRL_TV, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	
	private JPanel doPhoneEvent(){
		JPanel panel = new JPanel();
		JButton btnCall = new JButton("Phone: Called");
		panel.add(btnCall);
		btnCall.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map property = new Hashtable();
					Event event = new Event(PhoneSimulateTopic.PHONE_RING, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	
	private JPanel doCameraEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doCameraSmallerEvent(), BorderLayout.NORTH);
		panel.add(doCameraIsValidEvent(), BorderLayout.WEST);
		panel.add(doCameraDirectionEvent(), BorderLayout.EAST);
		return panel;			
	}
	
	private JPanel doCameraDirectionEvent(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Camera: Direction");
		panel.add(btn);
		btn.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(CameraSimulateTopic.PROPER_DIRECTION, CameraSimulateTopic.Constants.DIR_WEST);
					Event event = new Event(CameraSimulateTopic.CAMERA_CTRL_CURTAIN, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doCameraIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnSmaller = new JButton("Camera: IsValid");
		panel.add(btnSmaller);
		btnSmaller.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map property = new Hashtable();
					Event event = new Event(CameraDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doCameraSmallerEvent(){
		JPanel panel = new JPanel();
		JButton btnSmaller = new JButton("Camera: Smaller");
		panel.add(btnSmaller);
		btnSmaller.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(CameraSimulateTopic.PROPER_ACTION, CameraSimulateTopic.Constants.ACTION_SMALLER);
					Event event = new Event(CameraSimulateTopic.CAMERA_CTRL_TV, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;
		
	}
	
	private JPanel doTvAction(){
		JPanel panel = new JPanel();
		JButton btnUp = new JButton("Tv: upChannel");
		panel.add(btnUp);
		btnUp.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(TvDevice.class.getName());
				if(ref != null){
					TvDevice tv = (TvDevice) context.getService(ref);
					tv.upChannel();
					
				}
			}			
		});
		return panel;
	}
	
	
	private JPanel doMicEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doMicOpenEvent(), BorderLayout.WEST);
		panel.add(doMicCloseEvent(), BorderLayout.CENTER);
		panel.add(doMicIsValidEvent(), BorderLayout.EAST);
		return panel;		
	}
	
	private JPanel doMicIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnIsValid = new JButton("Mic: IsValid");
		panel.add(btnIsValid);
		btnIsValid.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(MicDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;
		
	}
	private JPanel doMicCloseEvent(){
		JPanel panel = new JPanel();
		JButton btnClose = new JButton("Mic: CloseEvent");
		panel.add(btnClose);
		btnClose.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(MicSimulateTopic.MIC_CLOSE, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;
		
	}
	private JPanel doMicOpenEvent(){
		JPanel panel = new JPanel();
		JButton btnOpen = new JButton("Mic: OpenEvent");
		panel.add(btnOpen);
		btnOpen.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(MicSimulateTopic.MIC_OPEN, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;
		
	}
	
	private JPanel doAirconTuneEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doAirconTuneModeEvent(), BorderLayout.WEST);
		panel.add(doAirconTuneWindEvent(), BorderLayout.CENTER);
		panel.add(doAirconTuneTemperatureEvent(), BorderLayout.EAST);
		return panel;			
	}
	private JPanel doAirconTuneTemperatureEvent(){
		JPanel panel = new JPanel();
		JButton btnTmp = new JButton("Aircondition: UpTemperatureEvent");
		panel.add(btnTmp);
		btnTmp.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					property.put(AirConDevice.EVENT_PROPER_CTRL, "Up");
					Event event = new Event(AirConDevice.EVENT_TOPIC_TUNETEMP, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	private JPanel doAirconTuneWindEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneWind = new JButton("Aircondition: TuneWindEvent");
		panel.add(btnTuneWind);
		btnTuneWind.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_TUNEWIND, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	private JPanel doAirconTuneModeEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneMode = new JButton("Aircondition: TuneModeEvent");
		panel.add(btnTuneMode);
		btnTuneMode.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_TUNEMODE, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	private JPanel doAirconTmpEvent(){
		JPanel panel = new JPanel();
		JButton btnTmp = new JButton("Aircondition: TemperatureEvent");
		panel.add(btnTmp);
		btnTmp.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_TEMP, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doAirconWindEvent(){
		JPanel panel = new JPanel();
		JButton btnWind = new JButton("Aircondition: WindEvent");
		panel.add(btnWind);
		btnWind.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_WIND, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	private JPanel doAirconModeEvent(){
		JPanel panel = new JPanel();
		JButton btnMode = new JButton("Aircondition: ModeEvent");
		panel.add(btnMode);
		btnMode.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_MODE, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	private JPanel doAirconIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnIsValid = new JButton("Aircondition: isValidEvent");
		panel.add(btnIsValid);
		btnIsValid.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doAirconPowerEvent(){
		JPanel panel = new JPanel();
		JButton btnPower = new JButton("Aircondition: PowerOn");
		panel.add(btnPower);
		btnPower.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(AirConDevice.EVENT_PROPER_ID, "1");
					property.put(AirConDevice.EVENT_PROPER_POWER_STATE, "1");
					Event event = new Event(AirConDevice.EVENT_TOPIC_POWER, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	
	
	
	
	private JPanel doCurtainEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doCurtainActionEvent(), BorderLayout.WEST);
		panel.add(doCurtainIsValidEvent(), BorderLayout.EAST);
		panel.add(doCurtainCurStateEvent(), BorderLayout.CENTER);
		return panel;		
	}
	
	private JPanel doCurtainCurStateEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Curtain: CurStateEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put("ID", "1");
					Event event = new Event(CurtainDevice.EVENT_TOPIC_CURSTATE, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doCurtainIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Curtain: IsValidEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put("ID", "1");
					Event event = new Event(CurtainDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doCurtainActionEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Curtain: OpenEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(CurtainDevice.EVENT_PROPER_ID, "1");
					property.put(CurtainDevice.EVENT_TOPIC_ACTION, "1");
					Event event = new Event(CurtainDevice.EVENT_TOPIC_ACTION, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doLightEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doLightIsValidEvent(), BorderLayout.WEST);
		panel.add(doLightPowerEvent(), BorderLayout.EAST);
		panel.add(doLightLevelEvent(), BorderLayout.CENTER);
		return panel;		
	}
	private JPanel doLightLevelEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Light:upLevelEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(LightDevice.EVENT_PROPER_ID, "1");
					property.put(LightDevice.EVENT_PROPER_LIGHT_LEVEL, LightDevice.Constants.LIGHT_LEVEL_TWO);
					Event event = new Event(LightDevice.EVENT_TOPIC_LEVEL, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doLightPowerEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Light:powerOnEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(LightDevice.EVENT_PROPER_ID, "1");
					property.put(LightDevice.EVENT_PROPER_POWER, LightDevice.Constants.POWER_ON);
					Event event = new Event(LightDevice.EVENT_TOPIC_POWER, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doLightIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Light:isValidEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(LightDevice.EVENT_PROPER_ID, "1");
					Event event = new Event(LightDevice.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doLightSensorEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doLightSensorIsValidEvent(), BorderLayout.WEST);
		panel.add(doLightSensorCurLevelEvent(), BorderLayout.EAST);
		//panel.add(doSetLight(), BorderLayout.EAST);
		return panel;	
		
	}
	private JPanel doLightSensorCurLevelEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("LightSensor:curLightLevel");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(LightSensor.EVENT_PROPER_ID, "1");
					Event event = new Event(LightSensor.EVENT_TOPIC_LIGHTLEVEL, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	private JPanel doLightSensorIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("LightSensor:isValid");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(LightSensor.EVENT_PROPER_ID, "1");
					Event event = new Event(LightSensor.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doThermoEvent(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doThermoIsValidEvent(), BorderLayout.WEST);
		panel.add(doThermoCurTmpEvent(), BorderLayout.CENTER);
		//panel.add(doSetLight(), BorderLayout.EAST);
		return panel;			
	}
	private JPanel doThermoCurTmpEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Thermo:curTemperature");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(ThermoSensor.EVENT_PROPER_ID, "1");
					Event event = new Event(ThermoSensor.EVENT_TOPIC_CURTEMP, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doThermoIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("Thermo:isValidEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					property.put(ThermoSensor.EVENT_PROPER_ID, "1");
					Event event = new Event(ThermoSensor.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;	
	}
	
	private JPanel doRFIDEvent(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("RFID:isValidEvent");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map property = new Hashtable();
					Event event = new Event(RFIDDetect.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;			
	}
	
	private JPanel doAirCon(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doTuneModel(), BorderLayout.NORTH);
		panel.add(doTuneWind(), BorderLayout.WEST);
		panel.add(doUpTemperature(), BorderLayout.EAST);
		panel.add(doDownTemperature(), BorderLayout.CENTER);
		panel.add(doAirConPower(), BorderLayout.CENTER);
		//panel.add(doSetTemperature(), BorderLayout.WEST);
		return panel;		
	}
	
	private JPanel doAirConPower(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doAirConPowerOn(), BorderLayout.WEST);
		panel.add(doAirConPowerOff(), BorderLayout.EAST);
		return panel;
	}
	
	private JPanel doAirConPowerOn(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("AirCondition:PowerOn");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.powerOn();
				}
			}			
		});
		return panel;	
		
	}
	
	private JPanel doAirConPowerOff(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("AirCondition:PowerOff");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.powerOff();
				}
			}			
		});
		return panel;	
		
	}
	
	private JPanel doTuneModel(){
		JPanel panel = new JPanel();
		JButton btnTuneModel = new JButton("AirCondition:TuneModel");
		panel.add(btnTuneModel);
		btnTuneModel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.tuneMode();
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doTuneWind(){
		JPanel panel = new JPanel();
		JButton btnTuneWind = new JButton("AirCondition:TuneWind");
		panel.add(btnTuneWind);
		btnTuneWind.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.tuneWind();
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doUpTemperature(){
		JPanel panel = new JPanel();
		JButton btnUpTemperature = new JButton("AirCondition:UpTemperature");
		panel.add(btnUpTemperature);
		btnUpTemperature.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.upTemperature();
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doDownTemperature(){
		JPanel panel = new JPanel();
		JButton btnDownTemperature = new JButton("AirCondition:DownTemperature");
		panel.add(btnDownTemperature);
		btnDownTemperature.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.downTemperature();
				}
			}			
		});
		return panel;		
	}
	
	/*
	private JPanel doSetTemperature(){
		JPanel panel = new JPanel();
		JButton btnSetTemperature = new JButton("AirCondition:SetTemperature");
		panel.add(btnSetTemperature);
		btnSetTemperature.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(AirConDevice.class.getName());
				if(ref != null){
					AirConDevice aircon = (AirConDevice) context.getService(ref);
					aircon.serTemperatrue(20);
				}
			}			
		});
		return panel;		
	}*/
	
	private JPanel doCurtain(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doCurtainOpen(), BorderLayout.WEST);
		panel.add(doCurtainClose(), BorderLayout.CENTER);
		//panel.add(doSetLight(), BorderLayout.EAST);
		return panel;		
	}
	
	private JPanel doCurtainOpen(){
		JPanel panel = new JPanel();
		JButton btnOpen = new JButton("Curtain:On");
		panel.add(btnOpen);
		btnOpen.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(CurtainDevice.class.getName());
				if(ref != null){
					CurtainDevice curtain = (CurtainDevice) context.getService(ref);
					curtain.open();
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doCurtainClose(){
		JPanel panel = new JPanel();
		JButton btnClose = new JButton("Curtain:Close");
		panel.add(btnClose);
		btnClose.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(CurtainDevice.class.getName());
				if(ref != null){
					CurtainDevice curtain = (CurtainDevice) context.getService(ref);
					curtain.close();
				}
			}			
		});
		return panel;		
	}
	private JPanel doLight(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doUpLight(), BorderLayout.WEST);
		panel.add(doDownLight(), BorderLayout.CENTER);
		panel.add(doLightPowerOn(), BorderLayout.SOUTH);
		panel.add(doLightPowerOff(), BorderLayout.EAST);
		//panel.add(doSetLight(), BorderLayout.EAST);
		return panel;		
	}
	
	private JPanel doLightPowerOn(){
		JPanel panel = new JPanel();
		JButton btnUpLevel = new JButton("Light:PowerOn");
		panel.add(btnUpLevel);
		btnUpLevel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(LightDevice.class.getName());
				if(ref != null){
					LightDevice light = (LightDevice) context.getService(ref);
					light.powerOn();
				}
			}			
		});
		return panel;
	}
	
	private JPanel doLightPowerOff(){
		JPanel panel = new JPanel();
		JButton btnUpLevel = new JButton("Light:PowerOff");
		panel.add(btnUpLevel);
		btnUpLevel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(LightDevice.class.getName());
				if(ref != null){
					LightDevice light = (LightDevice) context.getService(ref);
					light.powerOff();
				}
			}			
		});
		return panel;
	}
	
	/*private JPanel doSetLight(){
		JPanel panel = new JPanel();
		JButton btnSetLevel = new JButton("Light:SetLevel");
		panel.add(btnSetLevel);
		btnSetLevel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(LightDevice.class.getName());
				if(ref != null){
					LightDevice light = (LightDevice) context.getService(ref);
					light.setLevel(20);
				}
			}			
		});
		return panel;		
	}*/
	
	private JPanel doUpLight(){
		JPanel panel = new JPanel();
		JButton btnUpLevel = new JButton("Light:UpLevel");
		panel.add(btnUpLevel);
		btnUpLevel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(LightDevice.class.getName());
				if(ref != null){
					LightDevice light = (LightDevice) context.getService(ref);
					light.upLevel();
				}
			}			
		});
		return panel;			
	}
	
	private JPanel doDownLight(){
		JPanel panel = new JPanel();
		JButton btnDownLevel = new JButton("Light:DownLevel");
		panel.add(btnDownLevel);
		btnDownLevel.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				ref = context.getServiceReference(LightDevice.class.getName());
				if(ref != null){
					LightDevice light = (LightDevice) context.getService(ref);
					light.downLevel();
				}
			}			
		});
		return panel;			
	}
	
	private JPanel doLightSensor(){
		JPanel panel = new JPanel();
		JButton btnSense = new JButton("LightSensor:Sense");
		panel.add(btnSense);
		btnSense.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(LightSensorSimulateTopic.LIGHT_SENSE, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
	
	private JPanel doThermoSensor(){
		JPanel panel = new JPanel();
		JButton btnSense = new JButton("ThermoSensor:Sense");
		panel.add(btnSense);
		btnSense.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(ThermoSensorSimulateTopic.THERMO_SENSE, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;			
	}
	
	private JPanel doRFID(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(doRFIDDetect(), BorderLayout.WEST);
		panel.add(doRFIDVisit(), BorderLayout.EAST);
		panel.add(doRFIDIsValidEvent(), BorderLayout.CENTER);
		return panel;
	}
	
	private JPanel doRFIDIsValidEvent(){
		JPanel panel = new JPanel();
		JButton btnValid = new JButton("RFID: IsValidEvent");
		panel.add(btnValid);
		btnValid.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Map<String, String> property = new Hashtable<String, String>();
					Event event = new Event(RFIDDetect.EVENT_TOPIC_ISVALID, property);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;			
	}
	
	private JPanel doRFIDDetect(){
		JPanel panel = new JPanel();
		JButton btnDetect = new JButton("RFID:Detect");
		panel.add(btnDetect);
		btnDetect.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(RFIDSimulateTopic.RFID_DETECT, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;			
	}
	
	private JPanel doRFIDVisit(){		
		JPanel panel = new JPanel();
		JButton btnVisit = new JButton("RFID:Visit");
		panel.add(btnVisit);
		btnVisit.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				EventAdmin eventAdmin = (EventAdmin) tracker.getService();
				if(eventAdmin != null){
					Event event = new Event(RFIDSimulateTopic.RFID_VISIT, (Map)null);
					eventAdmin.postEvent(event);
				}
			}			
		});
		return panel;		
	}
}
